/**
 * JAVACC DEMO 1.0
 */
package com.apache.match.queue;

import java.io.Serializable;
import java.util.Map.Entry;
import java.util.Random;

/**
 * description:
 *
 * @author Administrator 创建时间：2017-1-6
 */
public class IndexList implements Serializable {

    /**
     * 根节点
     */
    protected Node root;

    /**
     * 阶数，M值
     */
    protected int order;

    /**
     * 叶子节点的链表头
     */
    protected Node head;

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public Object get(Comparable key) {
        return root.get(key);
    }

    public Object getMatch(Comparable key) {
        return root.getMatch(key);
    }

    public Node getNode(Comparable key) {
        return root.getNode(key);
    }

    public void remove(Comparable key) {
        root.remove(key, this);
    }

    public void insertOrUpdate(Comparable key, Object obj) {
        root.insertOrUpdate(key, obj, this);
    }

    public IndexList(int order) {
        if (order < 3) {
            System.out.print("order must be greater than 2");
            System.exit(0);
        }
        this.order = order;
        root = new Node(true, true);
        head = root;
    }

    public void printString(Node aNode) {
        for (Entry<Comparable, Object> entry : aNode.entries) {
            //System.out.println(entry.toString());

        }
        if (null != aNode.next)
            printString(aNode.next);
    }

    public void printString() {
        printString(this.head);
    }

    public static void testCreateIndexList() {
        System.out.println("Start Run!");
        IndexList il = new IndexList(6);
        Random random = new Random();
        long current = System.currentTimeMillis();
        //		for (int j = 0; j < 500000; j++) {

        for (int i = 0; i < 100; i++) {
            int randomNumber = random.nextInt(1000);
            int randomNumber2 = random.nextInt(1000);
            int randomNumber3 = random.nextInt(1000);
            Comparable key = new KeywordDesc(randomNumber, randomNumber2, "" + randomNumber3);
            il.insertOrUpdate(key, randomNumber3);
        }

        long duration = System.currentTimeMillis() - current;
        System.out.println("time elpsed for duration: " + duration);

        long duration2 = System.currentTimeMillis() - current - duration;
        System.out.println("time elpsed for duration: " + duration2);
        System.out.println(il.get(new KeywordDesc(80, 80, "1002")));
        System.out.println(il.getNode(new KeywordDesc(80, 80, "1002")));

        long duration3 = System.currentTimeMillis() - current - duration - duration2;
        System.out.println("time elpsed for duration: " + duration3);
        il.printString();
    }

    // 测试
    public static void main(String[] args) {
        testCreateIndexList();
    }
}
